/*

 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation; version 2
 of the License.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

*/
#import "SettingsView.h"
#import "XBMCControlApplication.h"
#import "Settings.h"


@implementation SettingsView

- (id) initWithApplication: (UIApplication*)app withAppID: (NSString*)appID withFrame: (struct CGRect)rect
{
   self = [super initWithFrame: rect];
   
   m_mainApp = (XBMCControlApplication*)app;

   // load defaults:
   m_settings = [Settings sharedInstance];
   
    // nav bar
    m_navBar = [[[UINavigationBar alloc] initWithFrame:CGRectMake(0.0f, 0.0f, rect.size.width, 48.0f)] autorelease];
    [m_navBar showLeftButton:nil withStyle:0 rightButton:@"Done" withStyle:3];
    [m_navBar setBarStyle:0];
    [m_navBar setDelegate:self];
    [self addSubview: m_navBar];

    // title
    UINavigationItem *title = [[[UINavigationItem alloc] initWithTitle:@"XBMC Settings"] autorelease];
    [m_navBar pushNavigationItem:title];

    // pref table
    m_table = [[UIPreferencesTable alloc] initWithFrame: CGRectMake(0.0f, 48.0f, rect.size.width, rect.size.height - 48.0f)];
    [m_table setDataSource:self];
    [m_table setDelegate:self];
    [self addSubview: m_table];
	
	 // create the cells
    m_ipAddressCell = [[UIPreferencesTextTableCell alloc] initWithFrame:CGRectMake(0.0f, 48.0f, rect.size.width, 48.0f)];
    [m_ipAddressCell setTitle:@"Xbox IP Address"];
    [m_ipAddressCell setValue:[m_settings getIPAddress]];
	[m_ipAddressCell setReturnAction:@selector(ipAddressSet)];
	[m_ipAddressCell setTarget:self];
	
	UITextField *tf = [m_ipAddressCell textField];
    [tf setText:[m_settings getIPAddress]];
    
	#ifndef __OBJC2__
	[tf setPreferredKeyboardType:1]; // crashes 2.0
    [tf setInitialSelectionBehavior:2]; // crashes 2.0
    [tf setAutoCapsType:0]; // crashes 2.0
    [tf setAutoEnablesReturnKey:YES];   // crashes 2.0
	#endif
	[tf setReturnKeyType:0];
    
	[m_ipAddressCell addSubview:tf];
	
    [m_table reloadData];
	
	return self;
}

- (void)ipAddressSet
{
   [m_table setKeyboardVisible:NO];
}

- (void) tableRowSelected:(NSNotification *)notification
{
    [[m_table cellAtRow: [m_table selectedRow] column:0] setSelected:NO];
}

- (int) numberOfGroupsInPreferencesTable: (id) table
{
    return 1;
}

- (int) preferencesTable:(id)table numberOfRowsInGroup:(int)group
{
    switch (group)
    {
        case 0:
            return 1;
		default:
			return nil;
    }
}

- (id)preferencesTable: (id)table cellForRow: (int) row inGroup:(int)group
{
    switch (group) {
        case 0:
            // only one item
            return m_ipAddressCell;
		default:
			return nil;
    }
}

- (id)preferencesTable:(id)table titleForGroup:(int)group
{
    switch (group)
    {
        case 0:
            return @"Network Settings";
		default:
			return nil;
    }
}

- (float)preferencesTable:(id)table heightForRow:(int)row inGroup:(int)group withProposedHeight:(float)proposedHeight;
{
	return 48.0f;
}

- (void)navigationBar:(id)navbar buttonClicked:(int)button
{
    switch (button)
    {
        case 0:
            [self saveAndExit];
            break;
        case 1:
            // do about
            // TODO
            break;
    }
}


-(void)saveAndExit
{
	// save defaults
    NSString *newIpAddress = [m_ipAddressCell value];
    if (![newIpAddress isEqualToString:[m_settings getIPAddress]])
    {
        [m_settings setIPAddress:newIpAddress];
    }
	
    [m_settings save];
	
	[m_mainApp transitionToMainMenu];
}


@end

